package com.jghz.controller;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jghz.framework.utils.DownloadUtils;
import com.jghz.service.LogService;

@RestController
public class LogController {

	@Autowired
	private LogService logService;

	@GetMapping("/logs")
	public Map<String, Object> getLogs(@RequestParam(value = "pageIndex") int pageIndex,
			@RequestParam(value = "pageSize") int pageSize) {
		return logService.getPageData(pageIndex, pageSize);
	}

	@GetMapping("/logs/download/{fileName:.+}")
	public void logDownload(@PathVariable("fileName") String fileName, HttpServletResponse response)
			throws IOException {
		String path = logService.getPath("transfer.log.path");
		File file = FileUtils.getFile(path, fileName);
		DownloadUtils.download(response, file, fileName);
	}

}
